﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DynamicDNS.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ServiceManagement Controller = ServiceManagement.Instance;
        public ConfigServiceClient Client = ConfigServiceClient.Instance;
        public MainWindow()
        {
            InitializeComponent();
            System.Windows.Forms.NotifyIcon notify = new System.Windows.Forms.NotifyIcon();
            notify.Icon = new System.Drawing.Icon("DynamicDNS.ico");
            notify.Visible = true;
            notify.Click += notify_Click;

            Msg.Content = "";
            
            Sheet.ItemsSource = ServiceController.GetServices();

        }

        void notify_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = WindowState.Normal;
        }

        private void Button_Close(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == System.Windows.WindowState.Minimized)
                this.Hide();
            base.OnStateChanged(e);
        }

        private void Button_Start(object sender, RoutedEventArgs e)
        {
            try
            {
                Controller.Start();
            }
            catch (Exception ex)
            {
                Msg.Content = ex.Message;
            }
        }

        private void Button_Stop(object sender, RoutedEventArgs e)
        {
            try
            {
                Controller.Stop();
            }
            catch (Exception ex)
            {
                Msg.Content = ex.Message;
            }
        }

        private void Button_Update(object sender, RoutedEventArgs e)
        {
              Sheet.DataContext = Client.UpdateAll(true);
            
        }

        private void Button_Save(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Cancel(object sender, RoutedEventArgs e)
        {

        }
    }
}
